package gui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;
import java.awt.Color;

public class CacheFrame {

	private JFrame frame;
	private JTextField size1;
	private JTextField line1;
	private JTextField m1;
	private JTextField time1;
	private JTextField size2;
	private JTextField line2;
	private JTextField m2;
	private JTextField time2;
	private JTextField size3;
	private JTextField line3;
	private JTextField m3;
	private JTextField time3;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CacheFrame window = new CacheFrame();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public CacheFrame() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 827, 500);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		
		JPanel cachePanel = new JPanel();
		cachePanel.setBounds(10, 11, 791, 440);
		frame.getContentPane().add(cachePanel);
		cachePanel.setLayout(null);
		
		JLabel lblCacheOrganization = new JLabel("Cache Organization:");
		lblCacheOrganization.setFont(new Font("OCR A Extended", Font.BOLD | Font.ITALIC, 16));
		lblCacheOrganization.setBounds(10, 11, 224, 33);
		cachePanel.add(lblCacheOrganization);
		
		JLabel lblCacheLevels = new JLabel("Cache Levels:");
		lblCacheLevels.setBounds(20, 55, 83, 14);
		cachePanel.add(lblCacheLevels);				
		
		JLabel level1 = new JLabel("Level 1:");
		level1.setBounds(35, 92, 46, 14);
		cachePanel.add(level1);
		
		JLabel lblSize = new JLabel("Size:");
		lblSize.setBounds(35, 117, 46, 14);
		cachePanel.add(lblSize);
		
		size1 = new JTextField();
		size1.setBounds(69, 114, 44, 20);
		cachePanel.add(size1);
		size1.setColumns(10);
		
		JLabel lblLine = new JLabel("Line:");
		lblLine.setBounds(138, 117, 46, 14);
		cachePanel.add(lblLine);
		
		line1 = new JTextField();
		line1.setBounds(175, 114, 44, 20);
		cachePanel.add(line1);
		line1.setColumns(10);
		
		JLabel lblAssotiativity = new JLabel("Associativity:");
		lblAssotiativity.setBounds(244, 117, 83, 14);
		cachePanel.add(lblAssotiativity);
		
		m1 = new JTextField();
		m1.setBounds(337, 114, 46, 20);
		cachePanel.add(m1);
		m1.setColumns(10);
		
		JLabel lblWritePolicy = new JLabel("Write Policy:");
		lblWritePolicy.setBounds(35, 142, 78, 14);
		cachePanel.add(lblWritePolicy);
		
		final JCheckBox wb1 = new JCheckBox("Write Back");
		wb1.setBounds(136, 138, 98, 23);
		cachePanel.add(wb1);
		
		final JCheckBox wth1 = new JCheckBox("Write Through");
		wth1.setBounds(254, 138, 107, 23);
		cachePanel.add(wth1);
		
		wb1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (wth1.isSelected()) {
					wth1.setSelected(false);
				}
			}
		});
		
		wth1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (wb1.isSelected()) {
					wb1.setSelected(false);
				}
			}
		});
		
		JLabel lblAccessTimein = new JLabel("Access time (in cycles):");
		lblAccessTimein.setBounds(35, 167, 149, 14);
		cachePanel.add(lblAccessTimein);
		
		time1 = new JTextField();
		time1.setBounds(207, 164, 46, 20);
		cachePanel.add(time1);
		time1.setColumns(10);
		
		final JLabel level2 = new JLabel("Level 2:");
		level2.setVisible(false);
		level2.setBounds(35, 192, 46, 14);
		cachePanel.add(level2);
		
		final JLabel sizeL2 = new JLabel("Size:");
		sizeL2.setVisible(false);
		sizeL2.setBounds(35, 217, 46, 14);
		cachePanel.add(sizeL2);
		
		size2 = new JTextField();
		size2.setBounds(69, 214, 44, 20);
		size2.setVisible(false);
		cachePanel.add(size2);
		size2.setColumns(10);
		
		final JLabel lineL2 = new JLabel("Line:");
		lineL2.setBounds(138, 217, 46, 14);
		lineL2.setVisible(false);
		cachePanel.add(lineL2);
		
		line2 = new JTextField();
		line2.setBounds(175, 214, 44, 20);
		line2.setVisible(false);
		cachePanel.add(line2);
		line2.setColumns(10);
		
		final JLabel assL2 = new JLabel("Associativity:");
		assL2.setBounds(244, 217, 83, 14);
		assL2.setVisible(false);
		cachePanel.add(assL2);
		
		m2 = new JTextField();
		m2.setBounds(339, 214, 44, 20);
		m2.setVisible(false);
		cachePanel.add(m2);
		m2.setColumns(10);
		
		final JLabel wp2 = new JLabel("Write Policy:");
		wp2.setBounds(35, 247, 78, 14);
		wp2.setVisible(false);
		cachePanel.add(wp2);
		
		final JCheckBox wb2 = new JCheckBox("Write Back");
		wb2.setBounds(136, 238, 98, 23);
		wb2.setVisible(false);
		cachePanel.add(wb2);
		
		final JCheckBox wth2 = new JCheckBox("Write Through");
		wth2.setBounds(254, 243, 107, 23);
		wth2.setVisible(false);
		cachePanel.add(wth2);
		
		wb2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (wth2.isSelected()) {
					wth2.setSelected(false);
				}
			}
		});
		
		wth2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (wb2.isSelected()) {
					wb2.setSelected(false);
				}
			}
		});
		
		final JLabel timeL2 = new JLabel("Acess time (in cycles):");
		timeL2.setBounds(35, 272, 149, 14);
		timeL2.setVisible(false);
		cachePanel.add(timeL2);
		
		time2 = new JTextField();
		time2.setBounds(209, 269, 44, 20);
		time2.setVisible(false);
		cachePanel.add(time2);
		time2.setColumns(10);
		
		final JLabel level3 = new JLabel("Level 3:");
		level3.setBounds(35, 297, 46, 14);
		level3.setVisible(false);
		cachePanel.add(level3);
		
		final JLabel sizeL3 = new JLabel("Size:");
		sizeL3.setBounds(35, 322, 46, 14);
		sizeL3.setVisible(false);
		cachePanel.add(sizeL3);
		
		size3 = new JTextField();
		size3.setBounds(69, 322, 44, 20);
		size3.setVisible(false);
		cachePanel.add(size3);
		size3.setColumns(10);
		
		final JLabel lineL3 = new JLabel("Line:");
		lineL3.setBounds(138, 322, 46, 14);
		lineL3.setVisible(false);
		cachePanel.add(lineL3);
		
		line3 = new JTextField();
		line3.setBounds(175, 319, 44, 20);
		line3.setVisible(false);
		cachePanel.add(line3);
		line3.setColumns(10);
		
		final JLabel assL3 = new JLabel("Associativity:");
		assL3.setBounds(244, 322, 83, 14);
		assL3.setVisible(false);
		cachePanel.add(assL3);
		
		m3 = new JTextField();
		m3.setBounds(339, 319, 44, 20);
		m3.setVisible(false);
		cachePanel.add(m3);
		m3.setColumns(10);
		
		final JLabel wp3 = new JLabel("Write Policy:");
		wp3.setBounds(35, 347, 78, 14);
		wp3.setVisible(false);
		cachePanel.add(wp3);
		
		final JCheckBox wb3 = new JCheckBox("Write Back");
		wb3.setBounds(136, 343, 98, 23);
		wb3.setVisible(false);
		cachePanel.add(wb3);
		
		final JCheckBox wth3 = new JCheckBox("Write Through");
		wth3.setBounds(249, 343, 112, 23);
		wth3.setVisible(false);
		cachePanel.add(wth3);
		
		wb3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (wth3.isSelected()) {
					wth3.setSelected(false);
				}
			}
		});
		
		wth3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (wb3.isSelected()) {
					wb3.setSelected(false);
				}
			}
		});
		
		final JLabel timeL3 = new JLabel("Acess time (in cycles):");
		timeL3.setBounds(35, 372, 149, 14);
		timeL3.setVisible(false);
		cachePanel.add(timeL3);
		
		time3 = new JTextField();
		time3.setBounds(209, 369, 44, 20);
		time3.setVisible(false);
		cachePanel.add(time3);
		time3.setColumns(10);			
				
		final JLabel req1 = new JLabel("all fields are required");
		req1.setFont(new Font("Tahoma", Font.ITALIC, 11));
		req1.setForeground(Color.RED);
		req1.setVisible(false);
		req1.setBounds(434, 117, 149, 14);
		cachePanel.add(req1);
		
		final JLabel w1 = new JLabel("selection required");
		w1.setFont(new Font("Tahoma", Font.ITALIC, 11));
		w1.setForeground(Color.RED);
		w1.setVisible(false);
		w1.setBounds(385, 142, 120, 14);
		cachePanel.add(w1);
		
		final JLabel t1 = new JLabel("time required");
		t1.setForeground(Color.RED);
		t1.setVisible(false);
		t1.setFont(new Font("Tahoma", Font.ITALIC, 11));
		t1.setBounds(295, 167, 98, 14);
		cachePanel.add(t1);
		
		final JLabel req2 = new JLabel("all fields are required");
		req2.setForeground(Color.RED);
		req2.setVisible(false);
		req2.setFont(new Font("Tahoma", Font.ITALIC, 11));
		req2.setBounds(434, 217, 120, 14);
		cachePanel.add(req2);
		
		final JLabel w2 = new JLabel("selection required");
		w2.setForeground(Color.RED);
		w2.setVisible(false);
		w2.setFont(new Font("Tahoma", Font.ITALIC, 11));
		w2.setBounds(385, 247, 98, 14);
		cachePanel.add(w2);
		
		final JLabel t2 = new JLabel("time required");
		t2.setForeground(Color.RED);
		t2.setVisible(false);
		t2.setFont(new Font("Tahoma", Font.ITALIC, 11));
		t2.setBounds(295, 272, 66, 14);
		cachePanel.add(t2);
		
		final JLabel req3 = new JLabel("all fields are required");
		req3.setForeground(Color.RED);
		req3.setVisible(false);
		req3.setFont(new Font("Tahoma", Font.ITALIC, 11));
		req3.setBounds(434, 322, 120, 14);
		cachePanel.add(req3);
		
		final JLabel w3 = new JLabel("selection required");
		w3.setForeground(Color.RED);
		w3.setVisible(false);
		w3.setFont(new Font("Tahoma", Font.ITALIC, 11));
		w3.setBounds(385, 347, 131, 14);
		cachePanel.add(w3);
		
		final JLabel t3 = new JLabel("time required");
		t3.setForeground(Color.RED);
		t3.setVisible(false);
		t3.setFont(new Font("Tahoma", Font.ITALIC, 11));
		t3.setBounds(295, 372, 88, 14);
		cachePanel.add(t3);
		
		final JComboBox levels = new JComboBox();		
		levels.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {				
				if (levels.getSelectedIndex() == 0) {
					level2.setVisible(false);
					sizeL2.setVisible(false);
					size2.setVisible(false);
					lineL2.setVisible(false);
					line2.setVisible(false);
					assL2.setVisible(false);
					m2.setVisible(false);
					wp2.setVisible(false);
					wb2.setVisible(false);
					wth2.setVisible(false);
					timeL2.setVisible(false);
					time2.setVisible(false);
					req2.setVisible(false);
					w2.setVisible(false);
					t2.setVisible(false);
					
					level3.setVisible(false);
					sizeL3.setVisible(false);
					size3.setVisible(false);
					lineL3.setVisible(false);
					line3.setVisible(false);
					assL3.setVisible(false);
					m3.setVisible(false);
					wp3.setVisible(false);
					wb3.setVisible(false);
					wth3.setVisible(false);
					timeL3.setVisible(false);
					time3.setVisible(false);
					req3.setVisible(false);
					w3.setVisible(false);
					t3.setVisible(false);
				} else if (levels.getSelectedIndex() == 1) {
					level2.setVisible(true);
					sizeL2.setVisible(true);
					size2.setVisible(true);
					lineL2.setVisible(true);
					line2.setVisible(true);
					assL2.setVisible(true);
					m2.setVisible(true);
					wp2.setVisible(true);
					wb2.setVisible(true);
					wth2.setVisible(true);
					timeL2.setVisible(true);
					time2.setVisible(true);
					
					level3.setVisible(false);
					sizeL3.setVisible(false);
					size3.setVisible(false);
					lineL3.setVisible(false);
					line3.setVisible(false);
					assL3.setVisible(false);
					m3.setVisible(false);
					wp3.setVisible(false);
					wb3.setVisible(false);
					wth3.setVisible(false);
					timeL3.setVisible(false);
					time3.setVisible(false);
					req3.setVisible(false);
					w3.setVisible(false);
					t3.setVisible(false);
				} else if (levels.getSelectedIndex() == 2) {
					level2.setVisible(true);
					sizeL2.setVisible(true);
					size2.setVisible(true);
					lineL2.setVisible(true);
					line2.setVisible(true);
					assL2.setVisible(true);
					m2.setVisible(true);
					wp2.setVisible(true);
					wb2.setVisible(true);
					wth2.setVisible(true);
					timeL2.setVisible(true);
					time2.setVisible(true);
					
					level3.setVisible(true);
					sizeL3.setVisible(true);
					size3.setVisible(true);
					lineL3.setVisible(true);
					line3.setVisible(true);
					assL3.setVisible(true);
					m3.setVisible(true);
					wp3.setVisible(true);
					wb3.setVisible(true);
					wth3.setVisible(true);
					timeL3.setVisible(true);
					time3.setVisible(true);
				} 
			}
		});		
		levels.setModel(new DefaultComboBoxModel(new String[] {"1", "2", "3"}));
		levels.setSelectedIndex(0);
		levels.setBounds(109, 55, 44, 20);
		cachePanel.add(levels);
		
		final JButton btnNext = new JButton("Next ->");
		btnNext.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				if (levels.getSelectedIndex() == 0) {
					boolean valid = true;					
					if (size1.getText().equals("") || m1.getText().equals("") || line1.getText().equals("")) {
						valid = false;
						req1.setVisible(true);
					} else {
						req1.setVisible(false);
					}
					if (wb1.isSelected() || wth1.isSelected()) {
						w1.setVisible(false);
					} else {
						valid = false;
						w1.setVisible(true);						
					}
					if (time1.getText().equals("")) {
						t1.setVisible(true);
						valid = false;
					} else {
						t1.setVisible(false);
					}
					if (valid) {
						// go to next
					}
				} else if (levels.getSelectedIndex() == 1) {
					boolean valid = true;					
					if (size1.getText().equals("") || m1.getText().equals("") || line1.getText().equals("")) {
						valid = false;
						req1.setVisible(true);
					} else {
						req1.setVisible(false);
					}
					if (wb1.isSelected() || wth1.isSelected()) {
						w1.setVisible(false);
					} else {
						valid = false;
						w1.setVisible(true);						
					}
					if (time1.getText().equals("")) {
						t1.setVisible(true);
						valid = false;
					} else {
						t1.setVisible(false);
					}
					if (size2.getText().equals("") || m2.getText().equals("") || line2.getText().equals("")) {
						valid = false;
						req2.setVisible(true);
					} else {
						req2.setVisible(false);
					}
					if (wb2.isSelected() || wth2.isSelected()) {
						w2.setVisible(false);
					} else {
						valid = false;
						w2.setVisible(true);						
					}
					if (time2.getText().equals("")) {
						t2.setVisible(true);
						valid = false;
					} else {
						t2.setVisible(false);
					}					
					if (valid) {
						// go to next
					}
				} else if (levels.getSelectedIndex() == 2) {
					boolean valid = true;					
					if (size1.getText().equals("") || m1.getText().equals("") || line1.getText().equals("")) {
						valid = false;
						req1.setVisible(true);
					} else {
						req1.setVisible(false);
					}
					if (wb1.isSelected() || wth1.isSelected()) {
						w1.setVisible(false);
					} else {
						valid = false;
						w1.setVisible(true);						
					}
					if (time1.getText().equals("")) {
						t1.setVisible(true);
						valid = false;
					} else {
						t1.setVisible(false);
					}
					if (size2.getText().equals("") || m2.getText().equals("") || line2.getText().equals("")) {
						valid = false;
						req2.setVisible(true);
					} else {
						req2.setVisible(false);
					}
					if (wb2.isSelected() || wth2.isSelected()) {
						w2.setVisible(false);
					} else {
						valid = false;
						w2.setVisible(true);						
					}
					if (time2.getText().equals("")) {
						t2.setVisible(true);
						valid = false;
					} else {
						t2.setVisible(false);
					}
					if (size3.getText().equals("") || m3.getText().equals("") || line3.getText().equals("")) {
						valid = false;
						req3.setVisible(true);
					} else {
						req3.setVisible(false);
					}
					if (wb3.isSelected() || wth3.isSelected()) {
						w3.setVisible(false);
					} else {
						valid = false;
						w3.setVisible(true);						
					}
					if (time3.getText().equals("")) {
						t3.setVisible(true);
						valid = false;
					} else {
						t3.setVisible(false);
					}
					if (valid) {
						// go to next
					}
				}
			}			
		});
		btnNext.setBounds(650, 400, 89, 23);
		cachePanel.add(btnNext);
		
	}	
}
